import { $ } from './jquery.js'
class telLogin {
    constructor() {
        this.t = null
    }
    init() {

        this.telName()//调用验证用户名是否已被注册
    }
    //1.验证用户名是否已被注册
    telName() {
        let telname = null
        let yzms = null
        let check = null

        let _this = this
        $('#tels').on('input', function () {
            // console.log($(this).val());
            $.ajax({
                url: 'http://127.0.0.1/SAMSUNGproject/backend/login.php',
                method: 'get',
                data: {
                    tel: $(this).val(),
                }
            }).then(function (index) {

                if (index !== 'errName') {
                    $('.telBcxx').addClass('icon-chenggong').css('color', '#42d876').html('')
                    telname = true
                } else {
                    $('.telBcxx').addClass('iconfont icon-shibai').css('color', '#333').html('未注册')

                    telname = false
                }
                telPan()
                console.log(telname);
            })
        })
        //随机一个验证码
        //判断验证码
        // let t = null
        //验证码返回；利用点击触发函数，让其随机生成一个验证码，因为无法定时器异步执行，无法拿到随机的值，使用回电函数拿值
        function yazrandom(callback) {
            setTimeout(() => {
                let arr = []
                for (let i = 48; i <= 57; i++) {
                    arr.push(String.fromCharCode(i))
                }
                for (let i = 65; i <= 90; i++) {
                    arr.push(String.fromCharCode(i))
                }
                for (let i = 97; i <= 122; i++) {
                    arr.push(String.fromCharCode(i))
                }
                function random(min, max) {
                    if (max < min) {
                        let temp = max
                        max = min
                        min = temp
                    }
                    return Math.round(Math.random() * (max - min) + min)
                }
                let yzmname = arr[random(0, 61)] + arr[random(0, 61)] + arr[random(0, 61)] + arr[random(0, 61)] + arr[random(0, 61)] + arr[random(0, 61)]
                return callback(yzmname)
            }, 1);
        }
        yazrandom(function (result) {
            $('.yz_txt').html(result)
        });
        //验证码的生成
        $('.password-btn').on('click', function () {
            //获取一个随机的验证码然后插入到页面中
            yazrandom(function (result) {
                $('.yz_txt').html(result)
            });

        })
        //验证码的验证
        $('.yzms').on('input', function () {
            // let touppercase = $('.yz_txt').html().toLowerCase()

            if ($('.yz_txt').html().toLowerCase() === $(this).val().toLowerCase()) {
                //正确
                $('.yzmyz').addClass('icon-chenggong').css('color', '#42d876').removeClass('icon-shibai')
                yzms = true


                _this.t = setInterval(() => {
                    $('.btnTxt').css({
                        top: -36,
                        opacity: 0
                    })
                    $('.btnTxt').stop(true).animate({
                        top: -28,
                        opacity: 1
                    }, 300);
                }, 1000)
            } else {
                //不正确
                $('.yzmyz').addClass('icon-shibai').css('color', '#333').removeClass('icon-chenggong')
                yzms = false
            }
            telPan()
            console.log(yzms);
        })
        // telPan()
        //滑过按钮添加样式
        $('.ggs').hover(function () {
            $(this).addClass('active')
        }, function () {
            $(this).removeClass('active')
        })
        //点击去逛一逛
        $('.ggs').on('click', function () {
            $('.nubus').css('display', 'none')
            location.href = 'http://127.0.0.1/SAMSUNGproject/frontend/index_default.html'
        })
        //点击登录
        $('.login_btn').on('click', function () {
            $('.nubuss').css('display', 'block')
            $('.nubuss').stop(true).animate({
                opacity: 1
            })
            localStorage.setItem('username', $('#tels').val())
        })




        $('#el_cheked').on('click', function () {
            console.log();
            if ($('#el_cheked').prop('checked')) {
                check = true
                clearInterval(_this.t)
            } else {
                check = false
            }
            telPan()
        })
        function telPan() {
            if (telname && yzms && check) {
                $('.login_btn').removeAttr('disabled')
            } else {
                // console.log('err');
                $('.login_btn').attr('disabled', 'disabled')
            }
        }
        //点击手机号码登录跳转手机号码登录页面
        // console.log($('.login_other_i a').eq(1));
        $('.login_other_i a').eq(0).on('click', function (e) {
            location.href = 'http://127.0.0.1/SAMSUNGproject/frontend/login.html'
        })
    }

}
export {
    telLogin
}